package com.example.loader.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.loader.R;

public class PageLoader extends LinearLayout {
	private static final String TAG = "PageLoader";
	public boolean isFirstPage = false;//标识是否是第一页
	public boolean isLastPage = false;//标识是否是最后一页
	public boolean bOnlyOnePage = false;//标识是否只有一页
	/**
	 * 提示用户下拉(上拉)加载的状态
	 */
	private static final int PULL_TO_REFRESH = 2;
	/**
	 * 松开刷新状态
	 */
	private static final int RELEASE_TO_REFRESH = 3;
	/**
	 * 正在刷新状态
	 */
	private static final int REFRESHING = 4;
	/**
	 * 上拉加载状态
	 */
	private static final int PULL_UP_STATE = 0;
	/**
	 * 下拉加载状态
	 */
	private static final int PULL_DOWN_STATE = 1;
	/**
	 * 用于记录最后一次获得的y坐标
	 */
	private int mLastMotionY;
	/**
	 * header view
	 */
	private View mHeaderView;
	/**
	 * footer view
	 */
	private View mFooterView;
	/**
	 * WebView
	 */
	private WebView mWebView;
	/**
	 * header view的高度
	 */
	private int mHeaderViewHeight;
	/**
	 * footer view的高度
	 */
	private int mFooterViewHeight;
	/**
	 * header view中的指针图片
	 */
	private ImageView mHeaderImageView;
	/**
	 * footer view中的指针图片
	 */
	private ImageView mFooterImageView;
	/**
	 * header view中的提示文字
	 */
	private TextView mHeaderTextView;
	/**
	 * footer view中的提示文字
	 */
	private TextView mFooterTextView;
	/**
	 * header view中的进度条
	 */
	private ProgressBar mHeaderProgressBar;
	/**
	 * footer view中的进度条
	 */
	private ProgressBar mFooterProgressBar;
	/**
	 * layout inflater
	 */
	private LayoutInflater mInflater;
	/**
	 * 用于标识header view当前的状态
	 */
	private int mHeaderState;
	/**
	 * 用于标识footer view当前的状态
	 */
	private int mFooterState;
	/**
	 * 用于标识拖动时的状态，PULL_UP_STATE标识上拉刷新状态，PULL_DOWN_STATE标识下拉刷新状态
	 */
	private int mPullState;
	/**
	 * 变为向下的箭头,改变箭头方向
	 */
	private RotateAnimation mFlipAnimation;
	/**
	 * 变为逆向的箭头,旋转
	 */
	private RotateAnimation mReverseFlipAnimation;
	/**
	 * footer refresh listener
	 */
	private OnFooterRefreshListener mOnFooterRefreshListener;
	/**
	 * footer refresh listener
	 */
	private OnHeaderRefreshListener mOnHeaderRefreshListener;

	/**
	 * 拖动监听
	 */
	private OnPullListener mOnPullListener;

	// private String[] forbiddenModel = new String[] { "M6", "M8", "M9", "MX",
	// "MX2" };
	// public boolean isForbidden = false;

	private Context context;
	/**
	 * HeaderView的整体布局
	 */
	private View mHeaderLayout;
	/**
	 * FooterView的整体布局
	 */
	private View mFooterLayout;

	/**
	 * 松开手指时的topMargin，用于缓慢收回HeaderView和FooterView
	 */
	private int tempMargin;

	public PageLoader(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public PageLoader(Context context) {
		super(context);
		this.context = context;
		init();
	}

	/**
	 * 初始化
	 * 
	 */
	private void init() {
		// isForbidden = isForbidden();
		// 定义箭头的旋转动画
		mFlipAnimation = new RotateAnimation(0, -180,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		mFlipAnimation.setInterpolator(new LinearInterpolator());
		mFlipAnimation.setDuration(250);
		mFlipAnimation.setFillAfter(true);

		// 定义箭头的反转动画
		mReverseFlipAnimation = new RotateAnimation(-180, 0,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
		mReverseFlipAnimation.setDuration(250);
		mReverseFlipAnimation.setFillAfter(true);

		mInflater = LayoutInflater.from(getContext());
		// header view 在此添加,保证是第一个添加到linearlayout的最上端
		addHeaderView();
	}

	/**
	 * 给视图添加header view
	 */
	private void addHeaderView() {
		// header view
		mHeaderView = mInflater
				.inflate(R.layout.pageloader_header, this, false);

		mHeaderLayout = mHeaderView.findViewById(R.id.pull_to_refresh_layout);

		mHeaderImageView = (ImageView) mHeaderView
				.findViewById(R.id.pull_to_refresh_image);
		mHeaderTextView = (TextView) mHeaderView
				.findViewById(R.id.pull_to_refresh_text);
		mHeaderProgressBar = (ProgressBar) mHeaderView
				.findViewById(R.id.pull_to_refresh_progress);
		// header layout
		measureView(mHeaderView);
		mHeaderViewHeight = mHeaderView.getMeasuredHeight();
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				mHeaderViewHeight);
		// 设置topMargin的值为负的header View高度,即将其隐藏在最上方
		params.topMargin = -(mHeaderViewHeight);

		// 根据主题切换颜色
		/*
		 * XcarThemeProvide.getInstance(context).setViewBackgroundColor(this,
		 * R.color.news_header_bg);
		 */

		addView(mHeaderView, params);

	}

	/**
	 * 给视图添加footer view
	 */
	private void addFooterView() {
		// footer view
		mFooterView = mInflater
				.inflate(R.layout.pageloader_footer, this, false);
		mFooterLayout = mFooterView.findViewById(R.id.pull_to_load_layout);
		mFooterImageView = (ImageView) mFooterView
				.findViewById(R.id.pull_to_load_image);
		mFooterTextView = (TextView) mFooterView
				.findViewById(R.id.pull_to_load_text);
		mFooterProgressBar = (ProgressBar) mFooterView
				.findViewById(R.id.pull_to_load_progress);
		// footer layout
		measureView(mFooterView);
		mFooterViewHeight = mFooterView.getMeasuredHeight();
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				mFooterViewHeight);

		addView(mFooterView, params);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		// footer view 在此添加保证添加到linearlayout中的最后
		addFooterView();
		initContentView();
	}

	/**
	 * 初始化主体视图
	 */
	private void initContentView() {
		int count = getChildCount();
		if (count < 3) {
			throw new IllegalArgumentException(
					"当前View中必须包含3个子View，并且WebView必须在第二个位置！");
		}
		View view = null;
		for (int i = 0; i < count - 1; ++i) {
			view = getChildAt(i);
			if (view instanceof WebView) {
				// finish later
				mWebView = (WebView) view;
			}
		}
		if (mWebView == null) {
			throw new IllegalArgumentException("布局中必须包含一个WebView!");
		}
	}

	/**
	 * 用于测量指定View的宽高参数
	 * 
	 * @param child
	 *            要测量的View
	 */
	public void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
					MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent e) {
		int y = (int) e.getRawY();
		switch (e.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// 首先拦截down事件,记录y坐标
			mLastMotionY = y;
			break;
		case MotionEvent.ACTION_MOVE:
			// deltaY>0 是向下运动, deltaY<0是向上运动
			int deltaY = y - mLastMotionY;
			if (isRefreshViewScroll(deltaY)) {
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			break;
		}
		return false;
	}

	/*
	 * 如果在onInterceptTouchEvent()方法中没有拦截(即onInterceptTouchEvent()方法中 return
	 * false)则由PageLoader的子View来处理;否则由下面的方法来处理(即由PageLoader自己来处理)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		/*
		 * if(isForbidden) return false;
		 */
		// 正在刷新的時候，禁止触摸拖动效果
		if (mHeaderState == REFRESHING) {
			return true;
		}

		int y = (int) event.getRawY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			// onInterceptTouchEvent已经记录
			// mLastMotionY = y;
			break;
		case MotionEvent.ACTION_MOVE:
			int deltaY = y - mLastMotionY;
			if(null != mOnPullListener){
				mOnPullListener.onPull(this);
			}
			if (mPullState == PULL_DOWN_STATE) {
				// PageLoader执行下拉
				headerPrepareToRefresh(deltaY);
			} else if (mPullState == PULL_UP_STATE) {
				// PageLoader执行上拉
				footerPrepareToRefresh(deltaY);
			}
			mLastMotionY = y;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			int topMargin = getHeaderTopMargin();
			tempMargin = topMargin;
			if (mPullState == PULL_DOWN_STATE) {
				if (topMargin >= 0) {
					// 开始刷新
					headerRefreshing();
				} else {
					// 还没有执行刷新，重新隐藏
					smoothSetTopMargin(-mHeaderViewHeight);
				}
			} else if (mPullState == PULL_UP_STATE) {
				if (Math.abs(topMargin) >= mHeaderViewHeight
						+ mFooterViewHeight) {
					// 开始执行footer 刷新
					footerRefreshing();
				} else {
					// 还没有执行刷新，重新隐藏
					smoothSetTopMargin(-mHeaderViewHeight);
				}
			}
			break;
		}
		return super.onTouchEvent(event);
	}

	/**
	 * 是否应该到了父View,即PageLoader滑动
	 * 
	 * @param deltaY
	 *            , deltaY > 0 是向下运动,deltaY < 0是向上运动
	 * @return
	 */
	private boolean isRefreshViewScroll(int deltaY) {
		if (mHeaderState == REFRESHING || mFooterState == REFRESHING) {
			return false;
		}
		if (mWebView != null) {
			if (deltaY > 0 && mWebView.getScrollY() == 0) {
				mPullState = PULL_DOWN_STATE;
				return true;
			} else if (deltaY < 0
					&& (int) (mWebView.getContentHeight() * mWebView.getScale()) == (mWebView
							.getHeight() + mWebView.getScrollY())) {
				mPullState = PULL_UP_STATE;
				return true;
			}
		}
		return false;
	}

	/**
	 * header 准备刷新,手指移动过程,还没有释放
	 * 
	 * @param deltaY
	 *            手指滑动的距离
	 */
	private void headerPrepareToRefresh(int deltaY) {
		// 当header view的topMargin>=0时，说明已经完全显示出来了,修改header view 的提示状态
		int newTopMargin = changingHeaderViewTopMargin(deltaY);
		if (bOnlyOnePage) {
			mHeaderTextView.setText(R.string.pageloader_only_one_page);
			mHeaderLayout.setVisibility(View.GONE);
			mHeaderImageView.setVisibility(View.GONE);
			mHeaderState = RELEASE_TO_REFRESH;
		} else if (isFirstPage) {
			mHeaderTextView.setText(R.string.pageloader_first_page);
			mHeaderLayout.setVisibility(View.GONE);
			mHeaderImageView.setVisibility(View.GONE);
			mHeaderState = RELEASE_TO_REFRESH;
		} else {
			mHeaderLayout.setVisibility(View.VISIBLE);
			mHeaderImageView.setVisibility(View.VISIBLE);
			// int newTopMargin = changingHeaderViewTopMargin(deltaY);
			if (newTopMargin >= 0 && mHeaderState != RELEASE_TO_REFRESH) {
				mHeaderTextView.setText(R.string.pull_to_refresh_release_label);
				mHeaderImageView.clearAnimation();
				mHeaderImageView.startAnimation(mFlipAnimation);
				mHeaderState = RELEASE_TO_REFRESH;
			} else if (newTopMargin < 0 && mHeaderState == RELEASE_TO_REFRESH) {// 拖动时没有释放
				mHeaderImageView.clearAnimation();
				mHeaderImageView.startAnimation(mReverseFlipAnimation);
				// mHeaderImageView.
				mHeaderTextView.setText(R.string.pull_to_refresh_pull_label);
				mHeaderState = PULL_TO_REFRESH;
			}
		}
	}

	/**
	 * footer 准备刷新,手指移动过程,还没有释放 移动footer view高度同样和移动header view
	 * 高度是一样，都是通过修改header view的topmargin的值来达到
	 * 
	 * @param deltaY
	 *            手指滑动的距离
	 */
	private void footerPrepareToRefresh(int deltaY) {
		int newTopMargin = changingHeaderViewTopMargin(deltaY);
		if (bOnlyOnePage) {
			mFooterTextView.setText(R.string.pageloader_only_one_page);
			mFooterLayout.setVisibility(View.GONE);
			mFooterImageView.setVisibility(View.GONE);
			mFooterState = RELEASE_TO_REFRESH;
		} else if (isLastPage) {
			mFooterTextView.setText(R.string.pageloader_last_page);
			mFooterLayout.setVisibility(View.GONE);
			mFooterImageView.setVisibility(View.GONE);
			mFooterState = RELEASE_TO_REFRESH;
		} else {
			mFooterLayout.setVisibility(View.VISIBLE);
			mFooterImageView.setVisibility(View.VISIBLE);
			// 如果header view topMargin 的绝对值大于或等于header + footer 的高度
			// 说明footer view 完全显示出来了，修改footer view 的提示状态
			if (Math.abs(newTopMargin) >= (mHeaderViewHeight + mFooterViewHeight)
					&& mFooterState != RELEASE_TO_REFRESH) {
				mFooterTextView
						.setText(R.string.pull_to_refresh_footer_release_label);
				mFooterImageView.clearAnimation();
				mFooterImageView.startAnimation(mFlipAnimation);
				mFooterState = RELEASE_TO_REFRESH;
			} else if (Math.abs(newTopMargin) < (mHeaderViewHeight + mFooterViewHeight)
					&& mFooterState == RELEASE_TO_REFRESH) {
				mFooterImageView.clearAnimation();
				mFooterImageView.startAnimation(mReverseFlipAnimation);
				mFooterTextView
						.setText(R.string.pull_to_refresh_footer_pull_label);
				mFooterState = PULL_TO_REFRESH;
			}
		}
	}

	/**
	 * 修改Header view top margin的值
	 */
	private int changingHeaderViewTopMargin(int deltaY) {
		LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
		float newTopMargin = params.topMargin + deltaY * 0.3f;
		// 这里对上拉做一下限制,因为当前上拉后然后不释放手指直接下拉,会把下拉刷新给触发了
		// 表示如果是在上拉后一段距离,然后直接下拉
		if (deltaY > 0 && mPullState == PULL_UP_STATE
				&& Math.abs(params.topMargin) <= mHeaderViewHeight) {
			return params.topMargin;
		}
		// 同样地,对下拉做一下限制,避免出现跟上拉操作时一样的bug
		if (deltaY < 0 && mPullState == PULL_DOWN_STATE && params.topMargin < 0
				&& Math.abs(params.topMargin) >= mHeaderViewHeight) {// 这里需要使用params.topMargin<0限定一下，否则会出现HeaderView被拉出后无法推回
			return params.topMargin;
		}
		params.topMargin = (int) newTopMargin;
		mHeaderView.setLayoutParams(params);
		invalidate();
		return params.topMargin;
	}

	/**
	 * header refreshing
	 */
	private void headerRefreshing() {
		mHeaderState = REFRESHING;
		setHeaderTopMargin(0);

		mHeaderImageView.setVisibility(View.GONE);
		mHeaderImageView.clearAnimation();
		mHeaderImageView.setImageDrawable(null);
		mHeaderProgressBar.setVisibility(View.VISIBLE);
		
		//在只有一页、首页、末页时，不设置“加载中...”状态，防止用于在HeaderView和FooterView缓慢收回过程中观察到文本的变化过程
//		if(!bOnlyOnePage&&!isFirstPage&&!isLastPage){
//			mHeaderTextView.setText(R.string.pull_to_refresh_refreshing_label);
//		}
		
		//header 在第一页、只有一页时，不显示 加载中...
		if(!bOnlyOnePage&&!isFirstPage){
			mHeaderTextView.setText(R.string.pull_to_refresh_refreshing_label);
		}

		if (mOnHeaderRefreshListener != null) {
			mOnHeaderRefreshListener.onHeaderRefresh(this);
		}

	}

	/**
	 * footer refreshing
	 */
	private void footerRefreshing() {
		mFooterState = REFRESHING;
		int top = mHeaderViewHeight + mFooterViewHeight;
		setHeaderTopMargin(-top);

		mFooterImageView.setVisibility(View.GONE);
		mFooterImageView.clearAnimation();
		mFooterImageView.setImageDrawable(null);
		mFooterProgressBar.setVisibility(View.VISIBLE);
		//在只有一页、首页、末页时，不设置“加载中...”状态，防止用于在HeaderView和FooterView缓慢收回过程中观察到文本的变化过程
//		if(!bOnlyOnePage&&!isFirstPage&&!isLastPage){
//			mFooterTextView.setText(R.string.pull_to_refresh_footer_refreshing_label);	
//		}
		if(!bOnlyOnePage&&!isLastPage){
			mFooterTextView.setText(R.string.pull_to_refresh_footer_refreshing_label);	
		}

		if (mOnFooterRefreshListener != null) {
			mOnFooterRefreshListener.onFooterRefresh(this);
		}

	}

	/**
	 * 设置header view 的topMargin的值
	 * 
	 * @param topMargin
	 *            为0时，说明header view刚好完全显示出来； 为-mHeaderViewHeight时，说明完全隐藏了
	 */
	private void setHeaderTopMargin(int topMargin) {
		LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
		params.topMargin = topMargin;
		mHeaderView.setLayoutParams(params);
		invalidate();
		
		mHeaderTextView.setText(R.string.pull_to_refresh_pull_label);
		mFooterTextView.setText(R.string.pull_to_refresh_footer_pull_label);
	}

	/**
	 * 与Handler一起使用，使用for循环逐渐改变Margin值，以实现缓慢收回HeaderView和FooterView
	 * @param topMargin
	 */
	private void smoothSetTopMargin(final int topMargin) {
		new Thread() {
			public void run() {
				if (Math.abs(tempMargin) <= mHeaderViewHeight && tempMargin < 0) {
					// 如果HeaderView的topMargin<HeaderView的高度，并且tempMargin<0，则证明HeaderView没有完全被拉出，则缓慢收回
					for (int i = tempMargin; i > topMargin; i--) {
						SystemClock.sleep(1);
						Message msg = new Message();
						msg.getData().putInt("temp", i);
						handler.sendMessage(msg);
					}
				} else if (tempMargin >= 0) {
					// 如果tempMargin>=0，则证明HeaderView完全被拉出，先瞬间返回只露出HeaderView的位置（topMargin=0）
					// 再缓慢收回HeaderView
					for (int j = 0; j > topMargin; j--) {
						SystemClock.sleep(1);
						Message msg = new Message();
						msg.getData().putInt("temp", j);
						handler.sendMessage(msg);
					}
				}

				/*
				 * topMargin<-mHeaderViewHeight,则证明footer开始被拉出
				 * topMargin<-(mHeaderViewHeight+mFooterViewHeight),证明footer被完全拉出
				 */
				if (Math.abs(tempMargin) >= mHeaderViewHeight && tempMargin < 0) {
					// Footer被拉出时，顶部Margin是在负向不断变大，所以当满足以下条件时，FooterView没有被完全拉出
					if (!(Math.abs(tempMargin) >= mHeaderViewHeight
							+ mFooterViewHeight)) {

						for (int i = tempMargin; i < topMargin; i++) {
							SystemClock.sleep(1);
							Message msg = new Message();
							msg.getData().putInt("temp", i);
							handler.sendMessage(msg);
						}
					} else {
						//该情况下，FooterView被完全拉出，先瞬间返回只露出FooterView的位置（topMargin=-(mHeaderViewHeight+mFooterViewHeight)）
						//再缓慢收回FooterView
						for (int i = -(mHeaderViewHeight + mFooterViewHeight); i < topMargin; i++) {
							SystemClock.sleep(1);
							Message msg = new Message();
							msg.getData().putInt("temp", i);
							handler.sendMessage(msg);
						}
					}
				}
			};
		}.start();
	}

	/**
	 * header view 完成更新后恢复初始状态
	 */
	public void onHeaderRefreshComplete() {
		setHeaderTopMargin(-mHeaderViewHeight);
		mHeaderImageView.setVisibility(View.VISIBLE);
		mHeaderImageView.setImageResource(R.drawable.news_header_arrow);
//		if(isCompleted){
//			mHeaderTextView.setText(R.string.pull_to_refresh_pull_label);
//			isCompleted = false;
//		}
		mHeaderProgressBar.setVisibility(View.GONE);
		mHeaderState = PULL_TO_REFRESH;
	}

	/**
	 * header view在刷新完成后恢复初始状态
	 * 
	 * @param lastUpdated
	 *            最后一次更新的时间
	 * 
	 */
	public void onHeaderRefreshComplete(CharSequence lastUpdated) {
		onHeaderRefreshComplete();
	}

	/**
	 * footer view 完成更新后恢复初始状态
	 */
	public void onFooterRefreshComplete() {
		setHeaderTopMargin(-mHeaderViewHeight);
		mFooterImageView.setVisibility(View.VISIBLE);
		mFooterImageView.setImageResource(R.drawable.news_footer_arrow);
//		if(isCompleted){
//			mFooterTextView.setText(R.string.pull_to_refresh_footer_pull_label);
//			isCompleted = false;
//		}
		mFooterProgressBar.setVisibility(View.GONE);
		mFooterState = PULL_TO_REFRESH;
	}

	/**
	 * 获取当前header view 的topMargin
	 * 
	 */
	private int getHeaderTopMargin() {
		LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
		return params.topMargin;
	}

	/**
	 * set headerRefreshListener
	 * 
	 */
	public void setOnHeaderRefreshListener(
			OnHeaderRefreshListener headerRefreshListener) {
		mOnHeaderRefreshListener = headerRefreshListener;
	}

	/**
	 * set footerRefreshListener
	 * 
	 */
	public void setOnFooterRefreshListener(
			OnFooterRefreshListener footerRefreshListener) {
		mOnFooterRefreshListener = footerRefreshListener;
	}

	/**
	 * 对外提供接口，用于WebView刷新时
	 */
	public interface OnFooterRefreshListener {
		public void onFooterRefresh(PageLoader view);
	}

	/**
	 * 对外提供接口，用于WebView刷新时
	 */
	public interface OnHeaderRefreshListener {
		public void onHeaderRefresh(PageLoader view);
	}

	/**
	 * 对外提供接口，用于拖动时的监听
	 * @author chenchong
	 *
	 * 2013-6-6
	 */
	public interface OnPullListener {
		public void onPull(PageLoader view);
	}

	public void setOnPullListener(OnPullListener onPullListener) {
		this.mOnPullListener = onPullListener;
	}

	/*
	 * private boolean isForbidden() { String model = Build.MODEL; Log.e(TAG,
	 * model); if(null!=model){ for (int i = 0; i < forbiddenModel.length; i++)
	 * { if(forbiddenModel[i].equalsIgnoreCase(model)){ return true;
	 * 
	 * } } } return false; }
	 */

	// 用于处理HeaderView的缓慢收回
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int temp = msg.getData().getInt("temp");
			LayoutParams params = (LayoutParams) mHeaderView.getLayoutParams();
			params.topMargin = temp;
			mHeaderView.setLayoutParams(params);
			invalidate();
			
			//在HeaderView和FooterView完全收回后，再将文本置为初始状态
			if(Math.abs(temp)+1 == mHeaderViewHeight||Math.abs(temp)-1 == mHeaderViewHeight){
				mHeaderTextView.setText(R.string.pull_to_refresh_pull_label);
				mFooterTextView.setText(R.string.pull_to_refresh_footer_pull_label);
			}
		};
	};
}